<? 
	include('base.php');
	User::protect();
	include_class('general_media');
	include_class('m2');
	
	$ma = MediaArea::get($_REQUEST['id']);
	if ($_GET['task'] == 'add_alias' && !db::isError($ma)) {
		$res = $ma->addMediaAlias($_POST['media']);
		if (!db::isError($res)) { ?>
			<script type="text/javascript">self.close(); opener.location.reload()</script>
		<?
		}
	}
	
	$page_title = 'Alias Media';
	$stylesheet = 'popup.css';
	include('layout/header_popup.php');
	
	$areaIDAdd = ($_GET['area']) ? $_GET['area'] : '0';
	if ($areaIDAdd) {
		$maAdd = MediaArea::get($areaIDAdd);
	}
	
	if (isset($maAdd) && !db::isError($maAdd)) { ?>
	
	<div id="breadcrumb">
		<a href="<?=$PHP_SELF?>?id=<?=$_GET['id']?>">Media&nbsp;&#62;</a>
		<? $breadcrumb = $maAdd->getAreaTrail();
			if ($breadcrumb) { 
				foreach($breadcrumb as $maBC) {
					if ($maBC->getID() == $maAdd->getID()) { 
						echo($maBC->getTitle() . '&nbsp;&#62;');
					} else {
						echo('<a href="' . $PHP_SELF . '?id=' . $_GET['id'] . '&amp;area=' . $maBC->getID() . '">' . $maBC->getTitle() . '&nbsp;&#62;</a>');
					}
				}
			}
		?>
	</div>
	<? }
	
	if (db::isError($res)) {
		$res->outputList();
	}
	
if (User::isAdmin()) { 
	if (!db::isError($ma)) { ?>
	
		<h1>areas:</h1>
			
		<? 
			$areas = (isset($maAdd) && !db::isError($maAdd)) ? $maAdd->getAreas() : MediaArea::getAreas();
			if ($areas) { ?>
				<ul class="sub-list">
				<? foreach($areas as $a) { ?>
					<li><b><a href="<?=$PHP_SELF?>?area=<?=$a->getID()?>&amp;id=<?=$ma->getID()?>"><?=$a->getTitle()?></a></b><? if ($a->getID() == $ma->getID()) { ?>&nbsp;(Current Area)<? } ?></li>
				<? } ?>
				</ul>
			<? } else {
				echo('<blockquote>None</blockquote>');
			} ?>
			
			<h1>media:</h1>
			<div class="inset">
			<form id="add_alias" action="<?=$PHP_SELF?>?task=add_alias&amp;id=<?=$_GET['id']?>" method="post">
			<? $media = ($maAdd) ? $maAdd->getMedia() : null;
			
			if ($media) { ?>
			<table class="list" border="0" cellspacing="1" cellpadding="0">
			<? foreach($media as $mi) { 
					?>
				<tr>
					<td><input type="checkbox" name="media[]" value="<?=$mi->getMediaID()?>"></td>
					<td style="white-space: nowrap" width="3000"><?=$mi->getTitle()?></td>
					<td><?=$mi->getType()?></td>
				</tr>
			
				<? } ?>	
			</table>	
			<? } else { ?>
			
				There are no media items in this area.
			
			<? } ?>
			</div>
			<br>
			<? if ($media) { ?><input type="submit" value="Add Alias" name="submit" class="button"><? } ?>
			<input type="submit" value="Exit and Close Window" name="exit" onclick="javascript:top.close(); return false" class="button">
			
			</form>		
		
	<? } else { 
	
		$ma->outputDialog(null, null); 
		
	}
} else { ?>

	Only an administrator may alias media.

<? } ?>

<? include('layout/footer_popup.php'); ?>
